<template>
    <el-dialog
            :title="!dataForm.relationId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="关联ID，主键" prop="relationId">
                        <el-input v-model.number="dataForm.relationId" placeholder="关联ID，主键"></el-input>
                </el-form-item>
                <el-form-item label="任务ID，关联任务表的任务ID" prop="taskId">
                        <el-input v-model.number="dataForm.taskId" placeholder="任务ID，关联任务表的任务ID"></el-input>
                </el-form-item>
                <el-form-item label="相关任务ID，关联任务表的任务ID" prop="relatedTaskId">
                        <el-input v-model.number="dataForm.relatedTaskId" placeholder="相关任务ID，关联任务表的任务ID"></el-input>
                </el-form-item>
                <el-form-item label="任务关联类型" prop="relationType">
                        <el-input v-model="dataForm.relationType" placeholder="任务关联类型"></el-input>
                </el-form-item>
                <el-form-item label="关联创建时间" prop="createdAt">
                        <el-date-picker
                                v-model="dataForm.createdAt"
                                type="datetime"
                                placeholder="关联创建时间">
                        </el-date-picker>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            relationId: null,
            taskId: null,
            relatedTaskId: null,
            relationType: null,
            createdAt: null
          },
          dataRule: {
            relationId: [
                        { required: true, message: '关联ID，主键不能为空', trigger: 'blur' }
            ],
            taskId: [
                        { required: true, message: '任务ID，关联任务表的任务ID不能为空', trigger: 'blur' }
            ],
            relatedTaskId: [
                        { required: true, message: '相关任务ID，关联任务表的任务ID不能为空', trigger: 'blur' }
            ],
            relationType: [
                        { required: true, message: '任务关联类型不能为空', trigger: 'blur' }
            ],
            createdAt: [
                        { required: true, message: '关联创建时间不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.relationId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.relationId = id || 0
            if (this.dataForm.relationId) {
              this.$http({
                url: this.$http.adornUrl(`/app/task_relation/info/${this.dataForm.relationId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.apptaskrelation
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/task_relation/${!this.dataForm.relationId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>